package org.cnbi.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.cnbi.dao.IHandleDao;
import org.cnbi.entity.IndicatorExpression;
import org.cnbi.entity.Kemu;
import org.cnbi.entity.Menu;
import org.cnbi.entity.User;
import org.cnbi.service.IHandleService;
import org.cnbi.utils.exception.BusinessException;
import org.cnbi.utils.pagination.PagingUtil;
import org.cnbi.utils.pojo.DynamicBean;
import org.cnbi.utils.pojo.Pagination;
import org.cnbi.utils.pojo.indicator.IndicatorParamsBean;
import org.cnbi.utils.sort.ReverseSort;
import org.cnbi.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("handleService")
public class IHandleServiceImpl implements IHandleService {

	@Autowired
	private IHandleDao handleDao;

	@Override
	public List<?> query(String sql, Object object) {
		try {
			return handleDao.query(sql, object);
		} catch (Exception e) {
			throw new BusinessException(e);
		}

	}

	@Override
	public List<?> query(String sql, Object object, Object paramObj) {
		try {
			return handleDao.query(sql, object, paramObj);
		} catch (Exception e) {
			throw new BusinessException(e);
		}

	}

	@Override
	public List<DynamicBean> queryDynamicBeanList(String sql, Object object) {
		try {
			return handleDao.queryDynamicBeanList(sql, object);
		} catch (Exception e) {
			throw new BusinessException(e);
		}

	}

	@Override
	public List<?> queryDynamicBeanList(String sql, Object object, boolean isDynamicBean) {
		try {
			return handleDao.queryDynamicBeanList(sql, object, isDynamicBean);
		} catch (Exception e) {
			throw new BusinessException(e);
		}

	}

	@Cacheable(value = "locationSearchCache", key = "#sql")
	@Override
	public List<Map> queryListMapBean(String sql, Object object) {
		try {
			List<Map> map = handleDao.queryListMapBean(sql, object);
			return map;
		} catch (Exception e) {
			throw new BusinessException(e);
		}

	}

	@Override
	public void execute(String sql, Object object) {
		try {
			handleDao.execute(sql, object);
		} catch (Exception e) {
			throw new BusinessException("执行>>>>>\n" + sql + "时出错了！" + e.getMessage());
		}

	}

	@Override
	public Pagination queryPagination(PagingUtil page, Object object) {
		try {
			return handleDao.queryPagination(page, object);
		} catch (Exception e) {
			throw new BusinessException("查询分页数据第【" + page.getCurrentPage() + "】页,每一页【" + page.getNumPerPage() + "】条记录时出错了！" + e.getMessage());
		}

	}

	@Override
	public int queryCount(String sql, Object obj) {
		try {
			return handleDao.queryCount(sql, obj);
		} catch (Exception e) {
			throw new BusinessException("查询总数时出错了！" + e.getMessage());
		}

	}

	@Override
	public boolean queryUserLicenseLastTimeWhetherPastdue(String sql) {

		try {
			return handleDao.queryUserLicenseLastTimeWhetherPastdue(sql);
		} catch (Exception e) {
			throw new BusinessException("检查license文件时出错了！" + e.getMessage());
		}
	}

	/*
	 * @data 2014年4月10日 复写的方法
	 * 
	 * @see org.cnbi.service.IHandleService#query()
	 */
	@Override
	public Object query() {
		try {
			return query("select", new User());
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}

	/*
	 * @data 2014年4月18日 复写的方法
	 * 
	 * @see org.cnbi.service.IHandleService#queryMenuTree(java.lang.String,
	 * org.cnbi.entity.Menu) List<DashboardDb> list = getModelChildren(sql,
	 * dashboard); if(null!=list && list.size()!=0){
	 * dashboard.setChildren(list); } if(list.size()==0){} return dashboard;
	 */
	@Override
	public Object queryMenuTree(String sql, Object obj) {
		try {
			List children = getMenuChildren(sql, obj);
			if (null != children && children.size() != 0) {
				if(obj instanceof Menu){
					Menu menu = (Menu)obj;
					menu.setChildren(children);
				}else if(obj instanceof Kemu){
					Kemu kemu = (Kemu)obj;
					kemu.setChildren(children);
				}
			}
			return obj;
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}

	private List getMenuChildren(String sql, Object obj) throws Exception {
		List list =  query(sql, obj);
		for (int i = 0; i < list.size(); i++) {
			Object o = list.get(i);
			List cl = getMenuChildren(sql, o);
			if (null != cl && cl.size() != 0) {
				if(o instanceof Menu){
					Menu menu = (Menu)o;
					menu.setChildren(cl);
				}else if(o instanceof Kemu){
					Kemu kemu = (Kemu)o;
					kemu.setChildren(cl);
				}
			}
		}
		return list;

	}

	@Override
	public int[] batchUpdate(String sql, List<Object[]> list) {

		try {
			return handleDao.batchUpdate(sql, list);
		} catch (Exception e) {
			throw new BusinessException("批量插入数据出错！" + e.getMessage());
		}
	}
	

	/*
	 * @data 2014年5月8日 复写的方法
	 * 
	 * @see org.cnbi.service.IHandleService#queryIndicatorMap()
	 */
	@Override
	public List<IndicatorExpression> queryIndicatorList(String sql, IndicatorExpression bean) {
		try {
			//long a = System.currentTimeMillis();
			List<IndicatorExpression> list = (List<IndicatorExpression>) handleDao.query(sql, bean);
			Map<IndicatorExpression, List<IndicatorExpression>> map = new LinkedHashMap<IndicatorExpression, List<IndicatorExpression>>();
			for (IndicatorExpression object : list){
				List<IndicatorExpression> children = getIndicatorChildren(sql, object);
				if (null != children && children.size() != 0) {
					if(object.getId() == 1){
						 object.setChildren(getTempChildren(children));
					}else{
					  object.setChildren(children);
					}
				}
				
			}
			//long b = System.currentTimeMillis();
			
			return list;
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}
    private List<IndicatorExpression> getTempChildren(List<IndicatorExpression> children){
    	List<IndicatorExpression> list = new ArrayList<IndicatorExpression>();
		String arr[] = StringUtil.get26Letter();
		for (String string : arr){
			IndicatorExpression tempBean = new IndicatorExpression(string);
			List<IndicatorExpression> cl = new ArrayList<IndicatorExpression>();
			for (IndicatorExpression cb : children) {
				if(string.equalsIgnoreCase(cb.getC())){
					cl.add(cb);
				}
			}
			tempBean.setChildren(cl);
			list.add(tempBean);
		}
		return list;
		
	}
	private List<IndicatorExpression> getIndicatorChildren(String sql, IndicatorExpression bean) throws Exception {
		List<IndicatorExpression> list = (List<IndicatorExpression>) query(sql, bean);
		int id = bean.getId();
		boolean flag = false;
		if(id == 2){
			flag = true;
		}
		if(id>139&& id<146){
			flag = true;
		}
		if (flag == true) {
			for (int i = 0; i < list.size(); i++) {
				IndicatorExpression o = list.get(i);
				List<IndicatorExpression> cl = getIndicatorChildren(sql, o);
				if (null != cl && cl.size() != 0) {
					o.setChildren(cl);
				}

			}
		}
		return list;

	}
   /**
    * 查询指标参数list的map
    */
	public Map<Integer, List<IndicatorParamsBean>> queryIndicatorParamsMap(String idsSql, String sql) {
		try {
			Map<Integer, List<IndicatorParamsBean>> indicatorParamsMap = new HashMap<Integer, List<IndicatorParamsBean>>();
			List<IndicatorParamsBean> ids = (List<IndicatorParamsBean>) handleDao.query(idsSql, new IndicatorParamsBean());
			for (IndicatorParamsBean obj : ids) {
				Integer integer = obj.getId();
				List<IndicatorParamsBean> params = (List<IndicatorParamsBean>) handleDao.query(sql, obj);
				Collections.sort(params, new ReverseSort(null,"desc"));
				indicatorParamsMap.put(integer, params);
			}
			return indicatorParamsMap;
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
	}

	/* 
	 * @data 2014年5月28日  复写的方法
	 * @see org.cnbi.service.IHandleService#queryBigDicamal(java.lang.String, java.lang.Object)
	 */
	@Override
	public BigDecimal queryBigDicamal(String sql, Object obj) {
		return handleDao.queryBigDicamal(sql, obj);
	}

	

}
